import { _decorator, Component, Node, Size, tween, UIOpacity, v3, view } from 'cc';
import { UIBase } from './UIBase';
const { ccclass, property } = _decorator;

@ccclass('UIAnimation')
export class UIAnimation{
private ui:UIBase=null;
private _viewSize:Size = view.getDesignResolutionSize();
constructor(uibase:UIBase){
    this.ui=uibase;
}
_objCallback={
    "open":[
        (cb:Function)=>{
            cb();
        },
        (cb:Function)=>{
            let Opacity=this.ui.node.addComponent(UIOpacity);
            Opacity.opacity=0;
            tween(Opacity)
            .to(0.3,{opacity:255})
            .call(()=>{
                cb();
            })
            .start()
        },
        (cb: Function) => {
            let pos = this.ui.node.position.clone();
            this.ui.node.setPosition(v3(this.ui.node.position.x, this._viewSize.height, 0));
            tween(this.ui.node)
                .to(0.3, { position: pos })
                .call(() => {
                    cb();
                })
                .start()
        },
    ],
    "close":[
        (cb: Function) => {
            cb();
        },
        (cb: Function) => {
            let Opacity = this.ui.node.getComponent(UIOpacity);
            Opacity.opacity = 255;
            tween(Opacity)
                .to(0.3, { opacity: 0 })
                .call(() => {
                    cb();
                })
                .start()
        },
        (cb: Function) => {
                tween(this.ui.node)
                .by(0.3,{position:v3(0,this._viewSize.height,0)})
                .call(()=>{
                    cb();
                })
                .start();
        },
    ]
}
    playAnimtion(openOrClose:string,index:number,cb:Function){
        //根据openOrClose去调用响应的动画逻辑。
        //动画播放完毕，那么执行回调。
        this._objCallback[openOrClose][index](cb);
    }
}


